package com.game.modular.game.playerMatch.service.base;

import com.game.modular.game.playerAssets.domain.PlayerHero;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class PvpNpcRanksBaseServiceImpl {
    private static final String cacheKey = "pvpMatch:%d:npcRanksInfo";;
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private HashOperations<Object, Object, List<PlayerHero>> hashOperations;

    public void savaCache(Long matchId, Long coreId, List<PlayerHero> entry) {
        String key1 = String.format(cacheKey, matchId);
        String key2 = coreId.toString();
        hashOperations.put(key1, key2, entry);
        redisTemplate.expire(key1, 120, TimeUnit.MINUTES);
    }

    public List<PlayerHero> selectCache(Long matchId, Long coreId) {
        String key1 = String.format(cacheKey, matchId);
        String key2 = coreId.toString();
        redisTemplate.expire(key1, 120, TimeUnit.MINUTES);
        return hashOperations.get(key1, key2);
    }
    public void removeCache(Long matchId) {
        String key1 = String.format(cacheKey, matchId);
        redisTemplate.delete(key1);
    }

    public void removeCache(Long matchId, Long coreId)  {
        String key1 = String.format(cacheKey, matchId);
        String key2 = coreId.toString();
        hashOperations.delete(key1, key2);
    }

}
